package self.zhangkang.chart

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import self.zhangkang.chart.databinding.FragmentKLineBinding

/**
 * @author：zhangkang19164
 * created on：2025/3/20 14:43
 * description：
 */
class KLineFragment : Fragment() {

    private val mStockDetailsViewModel: StockDetailsViewModel by activityViewModels()

    private val kLineViewModel: KLineViewModel by viewModels()

    private lateinit var kLineBinding: FragmentKLineBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val kLineBinding = FragmentKLineBinding.inflate(inflater, container, false)
        this.kLineBinding = kLineBinding
        return kLineBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        kLineBinding.kLineView.setOnTabNameClickListener { position, chartIndex ->
            Toast.makeText(
                requireContext(),
                "position: $position, chartIndex: ${chartIndex.indexName}",
                Toast.LENGTH_SHORT
            ).show()
        }
        mStockDetailsViewModel.shareStock.observe(viewLifecycleOwner) {
            kLineViewModel.setStock(it)
        }

        kLineViewModel.kLineLiveData.observe(viewLifecycleOwner) {
            kLineBinding.kLineView.setKLineData(it)
        }
    }
}